﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;

namespace Deferred.Editor.Gui
{
    public partial class ContentSaverWindow : Form
    {
        public string SelectedFile { get { return contentTreeView.SelectedNode.Tag.ToString(); } }
        public string SelectedFileWithoutExtension { get { return SelectedFile.Remove(SelectedFile.LastIndexOf('.')); } }
        public List<string> ContentItemPaths { get; set; }
        public List<string> ValidExtensions { get; set; }

        public ContentSaverWindow()
        {
            InitializeComponent();

            Shown += new EventHandler(ContentSaverWindow_Shown);
        }

        void ContentSaverWindow_Shown(object sender, EventArgs e)
        {
            contentTreeView.Nodes.Clear();
            if (ContentItemPaths != null)
            {
                foreach (string path in ContentItemPaths)
                {
                    if (MatchesAnyExtension(path, ValidExtensions)) AddPath(path, ValidExtensions);
                }
            }
        }

        public void AddPath(string path, List<string> validExtensions)
        {
            TreeNodeCollection currentNodeList = contentTreeView.Nodes;
            string[] pathParts = path.Split(Path.DirectorySeparatorChar);

            for (int i = 0; i < pathParts.Length; i++)
            {
                if (Path.HasExtension(pathParts[i]))
                {
                    if (MatchesAnyExtension(pathParts[i], validExtensions))
                    {
                        TreeNode newNode = currentNodeList.Add(pathParts[i]);
                        newNode.Tag = path;
                    }
                }
                else if (currentNodeList.ContainsKey(pathParts[i]))
                {
                    currentNodeList = currentNodeList[pathParts[i]].Nodes;
                }
                else
                {
                    TreeNode newNode = currentNodeList.Add(pathParts[i], pathParts[i]);
                    currentNodeList = newNode.Nodes;
                }
            }
        }

        private bool MatchesAnyExtension(string path, List<string> validExtensions)
        {
            return validExtensions.Contains(Path.GetExtension(path)) || validExtensions.Contains(Path.GetExtension(path).Substring(1));
        }

        private void contentTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (contentTreeView.SelectedNode.Tag == null)
            {
                okButton.Enabled = false;
            }
            else
            {
                okButton.Enabled = true;
            }
        }

        private void contentTreeView_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Tag != null)
            {
                contentTreeView.SelectedNode = e.Node;
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }
    }
}
